package com.example.myapplication;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter  extends BaseAdapter {
    List<PackageInfo> list; // 数据
    Context context;
    public MyAdapter(Context context){
        this.context = context;
        list = context.getPackageManager().getInstalledPackages(0);


    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 获取了list中position位置的数据

        // 通过布局文件创建视图对象
        View view = View.inflate(context, R.layout.list_item,null);
        // 找到子控件
        ImageView iv = view.findViewById(R.id.iv);
        // TODO 找到文本控件
        TextView tv = view.findViewById(R.id.tv);
        iv.setImageDrawable(
                list.get(position)
                .applicationInfo
                .loadIcon(context.getPackageManager())
        );
        // TODO 显示应用程序的名字
        tv.setText(list.get(position)
                .applicationInfo.loadLabel(context.getPackageManager()));

        return view;
    }
}
